package com.ruoyi.qywx.model.vo;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.StrUtils;
import com.ruoyi.qywx.entity.ContactWayEntity;
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : hcl
 * @Date : 2023/8/11 09:35
 */
@Getter
@Setter
public class ContactWayInfo {
    private Long id;
    private Long groupId;
    private String title;
    private int type;
    @NotEmpty(message = "请选择员工")
    private List<ContactVo> users;
    private boolean skipVerify;
    private String remark;
    private String dyRemark;
    private String qrcodeUrl;
    private List<TagVo> tags;
    private int scene;
    private int style;
    private Boolean guideJoin;
    private String desc;
    private String unfriendDesc;
    private String creator;
    private String iconUrl;
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String configId;
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date modifyTime;
    /**
     * 渠道码
     * 规则：teamId_类型_5位随机字符
     * 员工活码：LXW
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String state;

    public ContactWayEntity turnEntity(Long welcomeId, Long userId, Long teamId) {
        ContactWayEntity entity = new ContactWayEntity();
        entity.setId(id);
        entity.setTitle(title);
        entity.setConfigId(configId);
        entity.setDes(desc);
        entity.setGroupId(groupId);
        entity.setUnfriendDesc(unfriendDesc);
        entity.setDyRemark(dyRemark);
        entity.setRemark(remark);
        entity.setGuideJoin(guideJoin);
        entity.setIconUrl(iconUrl);
        entity.setQrcodeUrl(qrcodeUrl);
        entity.setType(type);
        entity.setSkipVerify(skipVerify);
        entity.setState(state);

        if (CollectionUtils.isNotEmpty(users)) {
            entity.setUsers(users.stream().map(ContactVo::getId).collect(Collectors.joining(StrUtil.COMMA)));
        }
        if (CollectionUtils.isNotEmpty(tags)) {
            entity.setTags(tags.stream().map(tag -> tag.getId().toString())
                    .collect(Collectors.joining(StrUtil.COMMA)));
        }

        entity.setWelcomeId(welcomeId);
        entity.setCreator(userId);
        entity.setTeamId(teamId);
        return entity;
    }

    /**
     * @param way      数据库数据
     * @param sysUser  创建人
     * @param userMaps 选择的员工
     * @param tagMap   选择的tag
     */
    public void editContactWay(ContactWayEntity way, SysUser sysUser, Map<String, String> userMaps, Map<Long, String> tagMap) {
        this.setId(way.getId());
        this.setGroupId(way.getGroupId());
        this.setTitle(way.getTitle());
        this.setType(way.getType());

        this.setCreator(StrUtils.defaultString(sysUser.getUserName(), "未知"));
        this.setModifyTime(way.getModifyTime());

        this.setDyRemark(way.getDyRemark());
        this.setRemark(way.getRemark());

        this.setIconUrl(way.getIconUrl());
        this.setSkipVerify(way.getSkipVerify());
        this.setStyle(way.getStyle());
        this.setGuideJoin(way.getGuideJoin());
        this.setDesc(way.getDes());
        this.setUnfriendDesc(way.getUnfriendDesc());

        if (CollectionUtils.isNotEmpty(userMaps)) {
            List<ContactVo> users = new ArrayList<>(userMaps.size());
            for (Map.Entry<String, String> map : userMaps.entrySet()) {
                users.add(new ContactVo(map.getKey(), map.getValue()));
            }
            this.setUsers(users);
        }
        if (CollectionUtils.isNotEmpty(tagMap)) {
            List<TagVo> tags = new ArrayList<>(tagMap.size());
            for (Map.Entry<Long, String> map : tagMap.entrySet()) {
                tags.add(new TagVo(map.getKey(), map.getValue()));
            }
            this.setTags(tags);
        }
    }
}
